/*
 * ConsPalabras.java
 *
 * Created on 10 de diciembre de 2008, 17:20
 */
package Vistas;

import Dominio.CtrlDominio;
import java.util.Collections;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author  sandra
 */
public class ConsPalabras extends javax.swing.JFrame {

    private CtrlDominio CD = CtrlDominio.getInstance();
    private Vector<String> palabras = new Vector<String>();
    private Vector<String> temas = new Vector<String>();
    private int idioma;
    private static String[] cabecerasTablaPalabra = {"Palabras"};
    private static String[] cabecerasTablaTemas = {"Temas"};

    /** Creates new form ConsultarPalabras */
    public ConsPalabras(int id) {
        idioma = id;
        palabras = CD.listarPalabras();
        Collections.sort(palabras);
        if (palabras.size() == 0) {
            JOptionPane.showMessageDialog(this, "No es posible consultar las palabras, porque no hay ninguna palabra en el diccionario", "No hay palabras en el diccionario", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
            new menu(idioma).setVisible(true);
        } else {
            initComponents();
            this.setName("Consultar Palabras");
            this.setLocationRelativeTo(null);
            Object[][] aux = new Object[palabras.size()][1];
            for (int i = 0; i < palabras.size(); i++) {
                aux[i][0] = palabras.get(i);
            }
            String fs = System.getProperty("file.separator");
            jPalabras.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaPalabra));
            ImageIcon ic = new ImageIcon(System.getProperty("user.dir") + fs + "src" + fs + "Image" + fs + "return.png");
            jBtSalir.setIcon(ic);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jPalabras = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jBtSalir = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTemas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPalabras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Palabras"
            }
        ));
        jPalabras.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPalabrasMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jPalabras);

        jBtSalir.setText("Volver");
        jBtSalir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jBtSalirMouseClicked(evt);
            }
        });
        jBtSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addComponent(jBtSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBtSalir)
                .addContainerGap())
        );

        jTemas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Temas"
            }
        ));
        jScrollPane3.setViewportView(jTemas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(35, 35, 35))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jBtSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtSalirActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jBtSalirActionPerformed

    private void jPalabrasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPalabrasMouseClicked
        int fila = jPalabras.getSelectedRow();
        if (fila == -1) {
            JOptionPane.showMessageDialog(this, "Selecciona una palabra para consultar", "No se ha seleccionado una palabra", JOptionPane.INFORMATION_MESSAGE);
        } else {
            String palabra = (String) jPalabras.getValueAt(fila, 0);
            temas = CD.listarTemaPalabra(palabra);
            if (temas.size() == 0) {
                Object[][] aux = new Object[0][0];
                jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTemas));
                JOptionPane.showMessageDialog(this, "La palabra " + palabra + " no tiene temas asociados a \u00e9l", "La palabra no tiene temas", JOptionPane.INFORMATION_MESSAGE);
            } else {
                Object[][] aux = new Object[temas.size()][1];
                for (int i = 0; i < temas.size(); i++) {
                    aux[i][0] = temas.get(i);
                }
                jTemas.setModel(new javax.swing.table.DefaultTableModel(aux, cabecerasTablaTemas));
            }
        }
    }//GEN-LAST:event_jPalabrasMouseClicked

    private void jBtSalirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jBtSalirMouseClicked
        this.dispose();
        new menu(idioma).setVisible(true);
    }//GEN-LAST:event_jBtSalirMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtSalir;
    private javax.swing.JTable jPalabras;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTemas;
    // End of variables declaration//GEN-END:variables
}
